package net.sea.commons.util;

import java.awt.Image;
import java.io.IOException;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;

import static java.util.regex.Pattern.compile;
import static javax.imageio.ImageIO.read;
import static java.util.regex.Pattern.CASE_INSENSITIVE;
import static java.awt.Image.SCALE_SMOOTH;

/**
 * 图片工具
 *
 * @author 华成伍
 */
public final class ImageUtils {

	public static final Pattern IMAGE = compile(
			"([\\s\\S]+?\\.jpg)|([\\s\\S]+?\\.jpge)|"
					+ "([\\s\\S]+?\\.gif)|([\\s\\S]+?\\.bmp)"
					+ "|([\\s\\S]+?\\.png)", CASE_INSENSITIVE);

	public static Image getImage(Class<?> c, String name) {
		try {
			return read(c.getResource(name));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static ImageIcon getImageIcon(Class<?> c, String name) {
		try {
			return new ImageIcon(read(c.getResource(name)));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static ImageIcon getImageIcon(Class<?> c, String name, int width,
										 int height) {
		return new ImageIcon(getImage(c, name, width, height));
	}

	public static Image getImage(Class<?> c, String name, int width, int height) {
		try {
			Image image = read(c.getResource(name));
			return image.getScaledInstance(width, height, SCALE_SMOOTH);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}
